import 'dart:async';

import 'package:flutter/material.dart';

class FutureLoopPage1 extends StatefulWidget {
  const FutureLoopPage1({Key? key}) : super(key: key);

  @override
  _FutureLoopPage1State createState() => _FutureLoopPage1State();
}

class _FutureLoopPage1State extends State<FutureLoopPage1> {
  ///声明变量
  late Timer _timer;
  ///记录当前的时间
  int currentTimer = 0;

  @override
  void initState() {
    super.initState();
    ///循环执行 间隔1秒
    _timer = Timer.periodic(Duration(milliseconds: 100), (timer) {
      ///自增
      currentTimer+=100;
      ///到5秒后停止
      if(currentTimer>=5000){
        _timer.cancel();
      }
      setState(() {

      });
    });
  }

  @override
  void dispose() {
    ///取消计时器
    _timer.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('倒计时'),
      ),
      body: Container(
        padding: EdgeInsets.all(20),
        width: double.infinity,
        height: double.infinity,
        child: Column(
          children: [
            ///层叠布局将进度与文字叠在一起
            Stack(
              ///子Widget居中
              alignment: Alignment.center,
              children: [
                //圆形进度
                CircularProgressIndicator(
                  //当前指示的进度 0.0-1.0，
                  value: currentTimer/5000,
                ),
                ///显示的文本
                Text("${(currentTimer/1000).toInt()}"),
              ],
            )
          ],
        ),
      ),
    );
  }
}
